package equals.exer;

/**
 * @author shkstart
 * @date 2022/5/14 - 16:41
 */
public class OrderTest {
    public static void main(String[] args) {
        Order o1 = new Order(13,"Jay");
        Order o2 = new Order(13,"Jay");
//        System.out.println(o1.equals(o2));
        //当我重写的equals里面的String类型比较用作==时,this.orderName==o1.orderName 答案是true
        //        Order o3 = new Order(13,"Jay");
        //        System.out.println(o1.equals(o3));
//      因为当String类型比较地址时，由于o1先前在常量池中就定义了Jay 所以o1与o3会指向同一个地址 所以答案为true
//        为什么我们在比较引用数据类型是否相同时还要用equals呢？
//        是因为我们在开发中不一定是向o1，o2，o3那样初始化属性，当我们this.orderName==o1.orderName还可能如下
//        Order o4 = new Order(13,new String("Jay"));   new String("Jay")这个应该是创造了新的地址值的Jay常量
//        System.out.println(o1.equals(o4)); //false


    }
}


class Order{
    private int orderId;
    private String orderName;

    public Order(int orderId,String orderName) {
        this.orderId = orderId;
        this.orderName = orderName;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if(obj instanceof Order) {
            Order o1 = (Order)obj;
            if (this.orderId == o1.orderId && this.orderName==o1.orderName) {
                return true;
            }else {
                return false;
            }
        }
        return false;
    }
}
